package researchServlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessions.LoginSystemRemote;
import sessions.abilitiesSession.AbilitySessionRemote;
import sessions.friendshipSession.FriendshipSessionRemote;
import sessions.helpSession.HelpRequestsessionRemote;
import sessions.requestFriendshipSession.FriendshipRequestSessionRemote;
import entities.Ability;
import entities.HelpRequest;
import entities.Utente;

public class UserResearchedServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		showUser(getUser(request), request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		showUser(getUser(request), request, response);
	}

	private Utente getUser(HttpServletRequest request) {
		Context jndiCtx;
		int id;
		try {
			id = Integer.parseInt(request.getParameter("id"));
			jndiCtx = new InitialContext();
			LoginSystemRemote loginSystemRemote = (LoginSystemRemote) jndiCtx
					.lookup("Progetto/LoginSystem/remote");
			return loginSystemRemote.getUserByID(id);
		} catch (Exception e) {
			System.out.println("Errore nel binding remoto");
			return null;
		}
	}

	// visualizza un utente
	private void showUser(Utente user, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Context jndiCtx;
		ArrayList<HelpRequest> helpGivenList;
		request.getSession().setAttribute("user", user);
		ArrayList<Ability> abilityList = new ArrayList<Ability>();
		try {
			jndiCtx = new InitialContext();
			AbilitySessionRemote asr = (AbilitySessionRemote) jndiCtx
					.lookup("Progetto/AbilitySession/remote");
			abilityList = asr.getUserAbilities(user.getId());
			HelpRequestsessionRemote hrs = (HelpRequestsessionRemote) jndiCtx
					.lookup("Progetto/HelpRequestsession/remote");
			helpGivenList = (ArrayList<HelpRequest>) hrs.receivedHelps(user.getId());
			request.getSession().setAttribute("helpGivenList", helpGivenList);
			//controlla se l'utente ricercato e' gia amico dell'utente del sistema
			if (request.getSession().getAttribute("id") != null){
				//se sono loggato
				FriendshipRequestSessionRemote friendshipRequestSession = (FriendshipRequestSessionRemote) jndiCtx
						.lookup("Progetto/FriendshipRequestSession/remote");
				FriendshipSessionRemote friendshipSession = (FriendshipSessionRemote) jndiCtx
						.lookup("Progetto/FriendshipSession/remote");
				if (friendshipSession.checkFriendships(user.getId(),
						(Integer) request.getSession().getAttribute("id"))){
					request.getSession().setAttribute("state", "friends");
				}else{
					if (friendshipRequestSession.checkRequestFriendships(user.getId(),
							(Integer) request.getSession().getAttribute("id"))){
						if(friendshipRequestSession.checkIndirectRequestFriendships(user.getId(),
								(Integer) request.getSession().getAttribute("id")))
							request.getSession().setAttribute("state", "indirectRequested");
						else
							request.getSession().setAttribute("state", "requested");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			request.getSession().setAttribute("state", "error");
		}
		request.getSession().setAttribute("userAbilities", abilityList);
		try {
			response.sendRedirect("/ProgettoWeb/Research/ResearchedUser.jsp");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
